﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;

namespace Movistar.SMS.OutlookAddIn
{
    public partial class ThisAddIn
    {
        private Office.CommandBar menuBar = null;
        private Office.CommandBarPopup bpMovistar = null;
        private Office.CommandBarButton bbConfig = null;
        private Office.CommandBarButton bbHistoric = null;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            AddMenuOption();
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        private void bbButton_Click(Office.CommandBarButton ctrl, ref bool cancel)
        {
            Form openForm = ((ctrl.Tag == "Movistar.SMS.OutlookAddIn.bbConfig") ? (Form)new ConfigForm() : (Form)new HistoricForm());

            if (openForm.DialogResult == DialogResult.Abort)
                MessageBox.Show("Se ha producido un error en Movistar SMS.\nPor favor, reinstale la aplicación.", "Movistar SMS",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
                if (openForm.ShowDialog() == DialogResult.Abort)
                    MessageBox.Show("Se ha producido un error en Movistar SMS.", "Movistar SMS",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        #region Menu Options Setup

        private void AddMenuOption()
        {
            try
            {
                menuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;

                bpMovistar = (Office.CommandBarPopup)menuBar.Controls.Add(
                    Office.MsoControlType.msoControlPopup, missing, missing, missing, true);

                if (bpMovistar != null)
                {
                    bpMovistar.Caption = "Movistar SMS";
                    bpMovistar.Tag = "Movistar.SMS.OutlookAddIn.bpMovistar";
                    bpMovistar.TooltipText = "Movistar SMS Outlook 2007 AddIn";

                    bbConfig = (Office.CommandBarButton)bpMovistar.Controls.Add(
                        Office.MsoControlType.msoControlButton, missing, missing, 1, true);
                    bbConfig.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
                    bbConfig.Caption = "Configuración Movistar SMS";
                    bbConfig.FaceId = 548;
                    bbConfig.Tag = "Movistar.SMS.OutlookAddIn.bbConfig";
                    bbConfig.Click += new Office._CommandBarButtonEvents_ClickEventHandler(bbButton_Click);

                    bbHistoric = (Office.CommandBarButton)bpMovistar.Controls.Add(
                        Office.MsoControlType.msoControlButton, missing, missing, 2, true);
                    bbHistoric.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
                    bbHistoric.Caption = "Histórico de Mensajes";
                    bbHistoric.FaceId = 584;
                    bbHistoric.Tag = "Movistar.SMS.OutlookAddIn.bbHistoric"; ;
                    bbHistoric.Click += new Office._CommandBarButtonEvents_ClickEventHandler(bbButton_Click);

                    bpMovistar.Visible = true;
                }
            }
            catch { }
        }

        #endregion

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}
